package com.ruoyi.wms.service.impl;

import java.util.List;

import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.MD5Utils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.wms.domain.DisassemblePipeline;
import com.ruoyi.wms.domain.EndLifeVehicleBase;
import com.ruoyi.wms.domain.QualityInspection;
import com.ruoyi.wms.domain.WmsOrder;
import com.ruoyi.wms.dto.EndLifeVehicleDto;
import com.ruoyi.wms.dto.UpdateEndLifeVehicleStatusDTO;
import com.ruoyi.wms.mapper.DisassemblePipelineMapper;
import com.ruoyi.wms.mapper.EndLifeVehicleMapper;
import com.ruoyi.wms.mapper.WmsOrderMapper;
import com.ruoyi.wms.service.IDisassemblePipelineService;
import com.ruoyi.wms.service.IEndLifeVehicleService;
import com.ruoyi.wms.service.IQualityInspectionService;
import com.ruoyi.wms.vo.EndLifeVehicleVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 报废车辆信息Service业务层处理
 *
 * @author libancang
 * @date 2025-09-12
 */
@Service
public class EndLifeVehicleServiceImpl implements IEndLifeVehicleService {
    @Autowired
    private EndLifeVehicleMapper endLifeVehicleMapper;

    @Autowired
    private IDisassemblePipelineService disassemblePipelineService;

    @Autowired
    private IQualityInspectionService qualityInspectionService;

    @Autowired
    private WmsOrderMapper wmsOrderMapper;
    /**
     * 查询报废车辆信息
     *
     * @param id 报废车辆信息主键
     * @return 报废车辆信息
     */
    @Override
    public EndLifeVehicleBase selectEndLifeVehicleById(String id) {
        return endLifeVehicleMapper.selectEndLifeVehicleById(id);
    }

    /**
     * 查询报废车辆信息列表
     *
     * @param endLifeVehicle 报废车辆信息
     * @return 报废车辆信息
     */
    @Override
    public List<EndLifeVehicleBase> selectEndLifeVehicleList(EndLifeVehicleDto endLifeVehicle) {
        List<EndLifeVehicleBase> endLifeVehicleBases = endLifeVehicleMapper.selectEndLifeVehicleList(endLifeVehicle);
        return endLifeVehicleBases;
    }

    /**
     * 新增报废车辆信息
     *
     * @param endLifeVehicle 报废车辆信息
     * @return 结果
     */
    @Override
    public int insertEndLifeVehicle(EndLifeVehicleBase endLifeVehicle) {
        endLifeVehicle.setId(MD5Utils.md5(endLifeVehicle.getPlateNo()));
//        endLifeVehicle.setCreateUser(SecurityUtils.getUsername());
//        endLifeVehicle.setCreateDate(DateUtils.getCurrentTime());
//        endLifeVehicle.setUpdateDate( DateUtils.getCurrentTime());
        return endLifeVehicleMapper.insertEndLifeVehicle(endLifeVehicle);
    }

    /**
     * 修改报废车辆信息
     *
     * @param endLifeVehicle 报废车辆信息
     * @return 结果
     */
    @Override
    public int updateEndLifeVehicle(EndLifeVehicleBase endLifeVehicle) {


        int updateResult = 0;
        if (StringUtils.isEmpty(endLifeVehicle.getId())) {
            updateResult = insertEndLifeVehicle(endLifeVehicle);
        }
//        endLifeVehicle.setUpdateDate( DateUtils.getCurrentTime());
        updateResult =endLifeVehicleMapper.updateEndLifeVehicle(endLifeVehicle);
        if (0 == updateResult) {
            updateResult = insertEndLifeVehicle(endLifeVehicle);
        }
        if("待查验".equalsIgnoreCase( endLifeVehicle.getStatus()) ){
//            状态完成后增加一条质检
            QualityInspection qi= new QualityInspection();
            BeanUtils.copyProperties( endLifeVehicle , qi);
            qualityInspectionService.insertQualityInspection(qi);
        }


//         车的保存是带查验，要去判断  ，把订单也改成带查验


        boolean flag = wmsOrderMapper.checkVehicleStatus(endLifeVehicle.getOrderNum());


        boolean flag2 = wmsOrderMapper.checkVehicleQualityInspectionStatus(endLifeVehicle.getOrderNum());
        if (flag &&  !flag2) {
            WmsOrder o = new WmsOrder();
            o.setId( endLifeVehicle.getOrderNum());
            o.setOrderStatus(  "待查验");
            o.setInspectionStatus(  "待查验");
            wmsOrderMapper.updateWmsOrder(o);
        }

        return updateResult;



    }

    /**
     * 批量删除报废车辆信息
     *
     * @param ids 需要删除的报废车辆信息主键
     * @return 结果
     */
    @Override
    public int deleteEndLifeVehicleByIds(String[] ids) {
        return endLifeVehicleMapper.deleteEndLifeVehicleByIds(ids);
    }

    /**
     * 删除报废车辆信息信息
     *
     * @param id 报废车辆信息主键
     * @return 结果
     */
    @Override
    public int deleteEndLifeVehicleById(String id) {
        return endLifeVehicleMapper.deleteEndLifeVehicleById(id);
    }

    @Override
    public List<EndLifeVehicleVo> queryProcurementLedgerList(EndLifeVehicleDto endLifeVehicle) {

        return endLifeVehicleMapper.queryProcurementLedgerList(endLifeVehicle);
    }

    @Override
    public int updateEndLifeVehicleStatus( UpdateEndLifeVehicleStatusDTO dto) {
        if("待拆解".equalsIgnoreCase( dto.getStatus())){
            String[] ids = dto.getIds();
            List<EndLifeVehicleVo>   EndLifeVehicleVoList=  endLifeVehicleMapper.selectEndLifeVehicleListByIds(ids);
//            把数据 放到流水线里面去 把数据存入到  wms_disassemble_pipeline
            for (EndLifeVehicleVo po : EndLifeVehicleVoList){
                DisassemblePipeline po1 =new DisassemblePipeline();
                BeanUtils.copyProperties(po,po1 );
                po1.setId(po.getVehicleId() );
                po1.setRawMaterialsStatus("待拆解" );
                po1.setOperationType("拆解");
                disassemblePipelineService.insertDisassemblePipeline(po1);
            }
        }
        return endLifeVehicleMapper.updateEndLifeVehicleStatus(dto );
    }
}
